#!/usr/bin/env bash

# This file is part of bashy2, an irc bot used on irc.sdf.org.

# Copyright (C) 2015-2016 mlaine@sdfeu.org, grugly@sdf.org
# Copyright (C) 2017 mlaine@sdfeu.org

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

#------------------------------------------------------------------------------

debug=1

server="$1"
irc_port=6667
irc_host='irc.sdf.org'
irc_pass="$HOME/.oline"
modes=12

cmd_dir='./bits/commands'

if [[ "$server" = 'us' ]]; then
    bot_user='BASHy2'
    bot_nick='BASHy2'
    bot_nick2='BASHy2-EU'
    bot_real='A BASH Bot'
elif [[ "$server" = 'eu' ]]; then
    bot_user="$USER"
    bot_nick='BASHy2-EU'
    bot_nick2='BASHy2'
    bot_real='BASHy2 in Europe'
else
    printf '\n%s\n\n' 'Supply argument us|eu if running from sdf-us|sdf-eu.'
    exit 1
fi

function init {
    source './bits/vitalbits.sh'
    source './bits/nonvitalbits.sh'
    exec 3<>"/dev/tcp/$irc_host/$irc_port" || return 1
    send 'user' "$bot_user" "$bot_real"
    send 'nick' "$bot_nick"
}

function recon {
    if (( "$i" <= '10' )); then
        sleep 30 && init
        let 'i++'
    else
        sleep 300 && init
    fi
}

function main {
    i=0; init
    while true; do
        IFS=$' \t\r' read -ru3 -a data
        if [[ ! "$data" ]]; then
            recon
        else
            debug 'IN:' "${data[@]}"
            if [[ "${data[0]}" = 'PING' ]]; then
                send 'pong' "${data[1]}"
                continue
            fi
            case "${data[1]}" in
                'PRIVMSG')
                    priv "${data[@]}" ;;
                'JOIN')
                    user_join "${data[@]}" ;;
                'QUIT')
                    user_quit "${data[@]}" ;;
                '001')
                    send 'oper' "$bot_user" "$(head -1 "$irc_pass")"
                    join && eval i=0
                    sleep 15 && chtopic &&
                    sleep 15 && serv 'upall' & ;;
                '332'|'331')
                    chtopic "${data[@]}" ;;
                '352'|'315')
                    upall "${data[@]}" ;;
                '311'|'319')
                    up "${data[@]}" ;;
            esac
        fi
    done
}

main

